package exam.bak.bilibili;

import java.util.Arrays;

public class P2 {
    public int[] GetSortedTasks(int[][] taskLists) {
        int len = taskLists.length * taskLists[0].length;
        int[] idxArray = new int[taskLists.length];
        int[] result = new int[len];
        for (int i = 0; i < len; i++) {
            int val = Integer.MAX_VALUE;
            int minIdx = -1;
            for (int j = 0; j < idxArray.length; j++) {
                int idx = idxArray[j];
                if (idx < taskLists[j].length && taskLists[j][idx] < val) {
                    val = taskLists[j][idx];
                    minIdx = j;
                }
            }
            result[i] = val;
            idxArray[minIdx]++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] taskLists = new int[][]{
                {1, 4, 5}, {1, 3, 4}, {2, 6, 7}
        };
        int[] ints = new P2().GetSortedTasks(taskLists);
        Arrays.stream(ints).forEach(System.out::print);
        System.out.println();
    }
}
